<?php


namespace app\api\service;


use think\Db;

/**
 * 药店钱包的业务
 * Class PharmacyWalletService
 * @package app\api\service
 */
class PharmacyWalletService
{

    /**
     * 更新药店的余额
     * @param $pharmacyId
     * @param $amount
     * @return bool
     */
    public function addOrUpdateWallet($pharmacyId, $amount){

        $selectSql = "select * from jn_pharmacy_wallet where pharmacy_id = ?";

        $data = Db::query($selectSql, [$pharmacyId]);

        if(empty($data)){

            $insertSql = "insert into jn_pharmacy_wallet(pharmacy_id, amount, created_at ) value (?, ?, now());";

            $result = Db::execute($insertSql, [$pharmacyId, $amount]);


        }else{

            $updateSql = "update jn_pharmacy_wallet set amount = amount+".$amount." where pharmacy_id = ?";

            $result = Db::execute($updateSql, [$pharmacyId]);

        }

        return 0 > $result;

    }

}